package org.wbd.pklib.web.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.wbd.pklib.db.Book;
import org.wbd.pklib.db.Item;
import org.wbd.pklib.db.Repo;
import org.wbd.pklib.db.RepoUser;
import org.wbd.pklib.db.User;
import org.wbd.pklib.db.logic.BookLogic;
import org.wbd.pklib.db.logic.RepoLogic;
/**
 * Ziarno zarządalne dostępne w zakresie sesji, wykorzystuje
 * dane z UserBean, używane przy wyszukiwaniu repozytoriów i 
 * książek  
 * @author Marcin
 *
 */
public class SearchBean {

	private Book bookToSearch= new Book();
	private User loggedUser;
	private Repo selectedRepo;
	private SelectItem item= new SelectItem();
	private Item selectedItem;
	
	private ArrayList<Item> searchScore;
	private ArrayList<Book> results;
	private Book selectedBook;

	
	public SearchBean() {

	}
	/**
	 * korzystając z logiki wyszukuje wszystkie książki z bazy i
	 * zapisuje rezultat w results
	 * @return 
	 */
	public String searchAllBooks(){
		BookLogic logic= BookLogic.getInstance();
		results= new ArrayList<Book>();
		List<Object> searchResult= logic.searchBooks(bookToSearch).getResultList();
		for(Object o : searchResult){
			results.add((Book)o);
		}
		return null;
	}
	/**
	 * uruchamia z logiki wyszukiwanie z parametrami pobranymi z 
	 * formularza , zapisuje wyniki w searchScore
	 * @return
	 */
	public String search(){

		searchScore = new ArrayList<Item>();
		//po wszystkich repo
		if (selectedRepo == null){  
			for (RepoUser r : loggedUser.getRepoUsers()){
				List<Object> searchResult = RepoLogic.getInstance().searchBooks(bookToSearch, r.getRepo().getRepoId()).getResultList();
				if (searchResult == null){
					return null;
				}
				for (Object i : searchResult){
					searchScore.add((Item) i);			
				}
			}
		}
		else { // po jednym
			List<Object> searchResult = RepoLogic.getInstance().searchBooks(bookToSearch, selectedRepo.getRepoId()).getResultList();
			if (searchResult == null){
				return null;
			}
			for (Object i : searchResult){
				searchScore.add((Item) i);			
			}
		}
		return null;
	}
	/**
	 * przekierowuje do wypożyczania
	 * @return
	 */
	public String loan(){
		return "loan.xhtml?faces-redirect=true";
		
	}
	/**
	 * przekierowuje do dołączania do repozytoriów
	 * @return
	 */
	public String redirect(){
		return "joinBook?faces-redirect= true";
	}

	public Item getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(Item selectedItem) {
		this.selectedItem = selectedItem;
	}

	public SelectItem getItem() {
		item.setValue(new Repo());
		item.setLabel("");
		return item;
	}

	public void setItem(SelectItem item) {
		this.item = item;
	}

	public Repo getSelectedRepo() {
		return selectedRepo;
	}

	public void setSelectedRepo(Repo selectedRepo) {
		this.selectedRepo = selectedRepo;
	}

	public ArrayList<Repo> getRepoList() {
		UserBean bean= (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userBean");
		return bean.getUserRepoList();
	}

	public Book getBookToSearch() {
		return bookToSearch;
	}

	public void setBookToSearch(Book bookToSearch) {
		this.bookToSearch = bookToSearch;
	}

	public ArrayList<Item> getSearchScore() {
		return searchScore;
	}

	public User getLoggedUser() {
		return loggedUser;
	}

	public void setLoggedUser(User loggedUser) {
		this.loggedUser = loggedUser;
	}

	public ArrayList<Book> getResults() {
		return results;
	}

	public Book getSelectedBook() {
		return selectedBook;
	}

	public void setSelectedBook(Book selectedBook) {
		this.selectedBook = selectedBook;
	}
	
	
	
}